package com.ssyc.student.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.bean.BusInfo;
import com.ssyc.common.utils.DateUtils;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.student.R;
import com.ssyc.student.adapter.StGvDetectionShowAnswerAdapter;
import com.ssyc.student.bean.ChooseAnswerInfo;
import com.ssyc.student.fragment.StudentPrepareDetectionCommitFragment;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * 学生单--预习检测--展示正确答案和错误答案的界面
 * Created by Administrator on 2018/5/10 0010.
 */

public class StudentShowPrepareDetectionAnswerActivity extends BaseActivity implements View.OnClickListener {

    public static final String SHOWANSWER = "StudentShowPrepareDetectionAnswerActivity.this";

    public static final int CLOSE = 0x166;

    //查看答案的消息标记
    public static final int LOOKANSWER = 0x110;

    //返回
    private ImageView ivBack;

    //用时
    private TextView tvTime;

    //展示正确或者错误答案的GridView
    private GridView gv;

    //重做
    private Button btRetry;

    //查看答案
    private Button btLookAnswer;

    //总用时间
    private String totalTime;

    //学生作答题目正确或者错误的集合Json字符串
    private String answerJson;

    //用于展示答题选项的数据集合
    private List<ChooseAnswerInfo> datas;

    //展示答题选项的列表适配器
    private StGvDetectionShowAnswerAdapter adapter;

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.student_activity_show_prepare_deteciton_answer;
    }

    @Override
    protected void init() {

        initView();

        showContent();

        //获取页面数据
        initData();

        //设置列表
        initGv();
    }


    /**
     * 初始化View的操作
     */
    private void initView() {

        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        tvTime = findViewById(R.id.tv_time);
        gv = findViewById(R.id.gv);
        btRetry = findViewById(R.id.bt_retry);
        btLookAnswer = findViewById(R.id.bt_look_answer);
        btRetry.setOnClickListener(this);
        btLookAnswer.setOnClickListener(this);

    }

    /**
     * 获取页面数据的方法
     */
    private void initData() {
        answerJson = getIntent().getStringExtra(SHOWANSWER);
        if (!TextUtils.isEmpty(answerJson)) {
            datas = GsonUtil.jsonToList(answerJson, ChooseAnswerInfo[].class);
        }

        //获取传递过来的时间
        long firstTime = getIntent().getLongExtra(StudentPrepareDetectionCommitFragment.FIRSTTIME, -1);
        //设置时间
        long time = System.currentTimeMillis() - firstTime;
        tvTime.setText("总用时： " + DateUtils.timeFormat(time));
    }

    /**
     * 设置展示正确或者错误答案的列表
     */
    private void initGv() {
        adapter = new StGvDetectionShowAnswerAdapter(this, datas, R.layout.student_gv_show_answer);
        gv.setAdapter(adapter);
    }

    /**
     * 页面控件点击事件
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.iv_back) {
            //通知预习检测StudentPrepareDetectionActivity界面关闭
            BusInfo busInfo = new BusInfo();
            busInfo.setType(CLOSE);
            EventBus.getDefault().post(busInfo);
            //返回
            finish();
        } else if (id == R.id.bt_retry) {
            //重试
        } else if (id == R.id.bt_look_answer) {
            //查看答案
            //发送查看答案的消息
            BusInfo info = new BusInfo();
            info.setType(LOOKANSWER);
            EventBus.getDefault().post(info);
            //关闭当前页面
            finish();
        }
    }
}
